﻿namespace Chain.Wechat
{
    using System;
    using System.IO;
    using System.Net;
    using System.Text;

    public class HttpRequestHelper
    {
        public string Reqeust(string url)
        {
            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
            request.Method = "GET";
            HttpWebResponse response = (HttpWebResponse) request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string responseText = reader.ReadToEnd();
            reader.Close();
            return responseText;
        }

        public string Reqeust(string url, string postText)
        {
            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            byte[] payload = Encoding.UTF8.GetBytes(postText);
            request.ContentLength = payload.Length;
            Stream writer = request.GetRequestStream();
            writer.Write(payload, 0, payload.Length);
            writer.Close();
            HttpWebResponse response = (HttpWebResponse) request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string responseText = reader.ReadToEnd();
            reader.Close();
            return responseText;
        }
    }
}

